package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

@RequestMapping("category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

//    根据id查询分类

    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") long id){
        // 根据id查询
        Category category = categoryService.getById(id);
//        // 把po转换为dto
//        CategoryDTO categoryDTO = new CategoryDTO(category);
        // 返回
        return ResponseEntity.ok(new CategoryDTO(category));
    }
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryList(@RequestParam("ids") List<Long> ids ){
        //查询
        List<Category> categorys = categoryService.listByIds(ids);
        // 转换
//        List<CategoryDTO> dtoList = new ArrayList<>(categorys.size());
//        for (Category category : categorys) {
//         dtoList.add (new CategoryDTO(category));
//
//        }
        List<CategoryDTO> dtoList = CategoryDTO.convertEntityList(categorys);
        // 返回
        return ResponseEntity.ok(dtoList);

    }

    //根据父类id查询子分类集合
    // pid 父类id
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>>  querCategoryByParent(@RequestParam("pid") Long pid){
        List<Category> list = categoryService.query().eq("parent_id", pid).list();
        //转换
        List<CategoryDTO> dtoList = CategoryDTO.convertEntityList(list);
        return ResponseEntity.ok(dtoList);
    }
    /*
     根据品牌id查询分类id集合
      @param brandId 品牌id
      @return 分类集合
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>>  querCategoryByBrand(@RequestParam("id")Long BrandId){
        // 查询
        List<CategoryDTO> list = categoryService.queryByBrand(BrandId);
//        返回
        return ResponseEntity.ok(list);
    }

}
